package net.lastcoder.ifserver.packet;

import net.lastcoder.ifserver.Constants;
import net.lastcoder.ifserver.util.DataTypeUtil;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * auth: WenYF
 * date: 2016/10/21
 */
public class SessionOpenPacket extends Packet {
    private byte type = TYPE_NONE;

    private byte protocolVersion = -1;
    private byte[] extra = new byte[0];;

    public SessionOpenPacket(byte type) {
        this.type = type;
    }

    public byte[] getExtra() {
        return extra;
    }

    public void setExtra(byte[] extra) {
        if (extra == null) {
            this.extra = new byte[0];
        } else {
            this.extra = extra;
        }
    }

    public byte getProtocolVersion() {
        return protocolVersion;
    }

    public void setProtocolVersion(byte protocolVersion) {
        this.protocolVersion = protocolVersion;
    }

    public int getType() {
        return type;
    }

//    public void setType(byte type) {
//        this.type = type;
//    }
//
//    public void setType(int type) {
//        this.type = (byte) type;
//    }

    @Override
    protected byte[] getContent(boolean isBinary) {
        if (isBinary) {
            return getBinary();
        } else {
            return getJson();
        }
    }

    private byte[] getJson(){
        JSONObject json = new JSONObject();
        try {
            switch (type) {
                case TYPE_SET:
                    json.put("protocolVersion", protocolVersion);
                default:{
                    json.put("type", type);
                    json.put("extra", new String(extra,charset));
                }

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return json.toString().getBytes();
    }

    private byte[] getBinary() {
        switch (type) {
            case TYPE_SET: {
                // 服务器主动发出的会话请求，不可能，仅仅用来测试
                // 登陆
                byte[] extraLength = DataTypeUtil.shortToByteArray((short) extra.length);
                byte[] data = new byte[1 + 1 + extraLength.length + extra.length];
                int count = 0, i;
                data[count++] = type;
                data[count++] = protocolVersion;
                for (i = 0; i < extraLength.length && count < data.length; i++, count++) {
                    data[count] = extraLength[i];
                }

                for (i = 0; i < extra.length && count < data.length; i++, count++) {
                    data[count] = extra[i];
                }

                return data;
            }
            default: {
                // 登陆结果返回
                byte[] extraLength = DataTypeUtil.shortToByteArray((short) extra.length);
                byte[] data = new byte[1 + extraLength.length + extra.length];
                int count = 0, i;
                data[count++] = type;
                for (i = 0; i < extraLength.length && count < data.length; i++, count++) {
                    data[count] = extraLength[i];
                }

                for (i = 0; i < extra.length && count < data.length; i++, count++) {
                    data[count] = extra[i];
                }

                return data;
            }
        }
    }

    @Override
    public byte getPacketType() {
        return Constants.PACKET_TYPE_SESSION_OPEN;
    }

    @Override
    public String toString() {
        return super.toString() + ", SessionOpenPacket{" +
                "type=" + type +
                ", protocolVersion=" + protocolVersion +
                ", extra=" + new String(extra, charset) +
                '}';
    }
}
